<html>
    <head>
        <title>IoT City Test Portal</title>
        <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script src="https://code.highcharts.com/stock/highstock.js"></script>
        <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>

        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  
        <script src="{{ url_for("static", filename="ws.js") }}"></script>
        
        <link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic' rel='stylesheet' type='text/css'>
        <link href='{{ url_for("static", filename="style.css") }}' rel='stylesheet' type='text/css'>
    </head>
    <body>
        <script type="text/javascript">
            var service_url="http://localhost:8000/authenticated";
            var token=localStorage.getItem('access_token');

            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status != 200) {
                        window.location.replace("/login");
                        return false;
                    }
                    return true;
                }
            }
            xhr.open("GET", service_url, true);
            xhr.setRequestHeader('Authorization', 'Bearer ' + token);
            xhr.send();
        </script>

        <div class="sidenav">
            <a href="/">Home</a>
            <a href="/verticals">Verticals</a>
            <a href="/devices">Devices</a>
            <a href="/subscriptions">Subscriptions</a>
            <a href="/alerts">Alerts</a>
            <a href="/triggered">Triggered</a>
        </div>

        <div body style="margin-left: 200px;">

            <table id="triggered">
                <tr>
                    <td>ID</td>
                    <td>Alert ID</td>
                    <td>Time</td>
                    <td>Dismiss</td>
                </tr>
            </table>

        </div>


        <script type="text/javascript">
            var service_url="http://localhost:8000"
            var url = service_url+'/triggered';
            $.ajax({ url: url,
                headers: {'Authorization': 'Bearer ' + localStorage.getItem('access_token')},
                success: function(data) {
                    values = data;
                    var table = document.getElementById("triggered");
                    for(value in values){
                        element = values[value];
                        

                        var row = table.insertRow(-1);
                        var cell1 = row.insertCell(0);
                        var cell2 = row.insertCell(1);
                        var cell3 = row.insertCell(2);
                        var cell4 = row.insertCell(3);

                        var button = document.createElement("button");
                        (function(id){
                            button.addEventListener('click', function(){ dismissTriggeredAlert(id); }, true);
                        })(element["id"]);
                        button.innerHTML = "Dismiss";
                            
                        cell1.innerHTML = element["id"];
                        cell2.innerHTML = element["alert_id"];
                        cell3.innerHTML = element["trigger_time"];
                        cell4.appendChild(button);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                }
            });

            function dismissTriggeredAlert(id){
                var xhr = new XMLHttpRequest();
                xhr.open("PUT", service_url+"/triggered/" + id + "/dismiss", true);
                xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
                xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('access_token'));
                xhr.send();
                xhr.onloadend = function () {
                    location.reload();
                };
            }
            
        </script>

    </body>
</html>

